//
//  PlayerViewController.m
//  MusicK
//
//  Created by 陈尚飞 on 15/11/30.
//  Copyright © 2015年 陈尚飞. All rights reserved.
//

#import "PlayerViewController.h"

@interface PlayerViewController ()

@end

@implementation PlayerViewController

-(void)loadView {
    self.view = [[UIView alloc] init];
    
    self.view.layer.anchorPoint = CGPointMake(0.5, 1.0);
    self.view.backgroundColor = [UIColor whiteColor];
    self.view.frame = [UIScreen mainScreen].bounds;
    
    UIPanGestureRecognizer *recongnize = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(moveAtcion:)];
    
    [self.view addGestureRecognizer:recongnize];

}
-(void)viewDidLoad {
    [super viewDidLoad];
}
-(void)moveAtcion :(UIPanGestureRecognizer *)recognize{
    CGPoint point = [recognize translationInView:self.view];
    CGAffineTransform transform = self.view.transform;
    
    //判断手势的状态
    if (recognize.state == UIGestureRecognizerStateEnded || recognize.state == UIGestureRecognizerStateCancelled) {
        
        if (ABS(transform.b) > 0.25) {
            
            [self dismissViewControllerAnimated:YES completion:nil];
        }else {
            [UIView animateKeyframesWithDuration:0.25
                                           delay:0.0
                                         options:0
                                      animations:^{
                                          self.view.transform = CGAffineTransformIdentity;
                                      } completion:^(BOOL finished) {
                                          
                                      }];
        
        }
    }else {
    
        CGFloat viewAngle = atan2(transform.b, transform.a);
        CGFloat dx = point.x * cos(viewAngle);
        CGFloat dy = point.y * sin(viewAngle);
        CGFloat angle = (dx + dy) / self.view.bounds.size.width;
        //倾斜视图
        transform = CGAffineTransformRotate(transform, angle);
        //平移视图
        transform.tx += 2 * (dx + dy);
        self.view.transform = transform;
        [recognize setTranslation:CGPointZero inView:self.view];
        
    }
}

@end
